package com.geekaca.ssm.controller;

import com.geekaca.ssm.domain.Brand;
import com.geekaca.ssm.exception.BusinessException;
import com.geekaca.ssm.service.BrandService;
import com.geekaca.ssm.utils.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    // 查询所有品牌
    @RequestMapping("/all")
    public Result getAll() {
        List<Brand> brandList = brandService.getAll();
        Result result = new Result();
        if (brandList != null && brandList.size() > 0) {
            result.setCode(Code.GET_0K);
            result.setData(brandList);
            result.setMessage("查询成功");
        } else {
            result.setCode(Code.GET_ERR);
            result.setMessage("没有找到符合条件的数据");

        }
        return result;
    }

    // 模糊，分页，查询所有品牌 查询所有品牌
    @RequestMapping("/allBrands/{pageNo}/{pageSize}")
    @ResponseBody
//    public Result getAllBrands(@RequestBody Brand brand, Integer start, Integer pageSize) {
    public Result getAllBrands(@RequestBody Brand brand, @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        List<Brand> brandList = brandService.getAllBrands(brand, pageNo, pageSize);


        Result result = new Result();
        if (brandList != null && brandList.size() > 0) {
            result.setCode(Code.GET_0K);
            result.setData(brandList);
            result.setMessage("查询成功");
        } else {
            result.setCode(Code.GET_ERR);
            result.setMessage("没有找到符合条件的数据");

        }
        return result;
    }


    // 添加品牌  统一返回格式
    @RequestMapping("/addBrand")
    @ResponseBody
    public Result addBrand(@RequestBody Brand brand) {
        String brandName = brand.getBrandName();
        Result result = new Result();

        // 如果品牌名字为空抛出异常统一处理，不继续添加操作
        if(brandName==null){
//            result= new Result(Code.BRANDNAME_NULL,"品牌名称为空",null);
            throw new BusinessException(Code.BRANDNAME_NULL,"品牌名称为空");
        }
        // 品牌名称非空，继续添加
        Integer i = brandService.addBrand(brand);
        if (i > 0) {
            result= new Result(Code.SAVE_0K,"添加成功",i);
        } else {
            result= new Result(Code.SAVE_ERR,"添加失败",i);
        }
        return result;
    }

    // 删除品牌
    @RequestMapping("/delBrand")
    @ResponseBody
    public String delBrand(@RequestBody Brand brand) {
        int i = brandService.delBrand(brand);
        if (i > 0) {
            return "OK";
        } else {
            return "fail";
        }
    }

    // 修改品牌
    @RequestMapping("/updateBrand")
    @ResponseBody
    public String updateBrand(@RequestBody Brand brand) {
        Integer i = brandService.updateBrand(brand);
        if (i != null && i > 0) {
            return "OK";
        } else {
            return "fail";
        }
    }

    // 根据id查品牌详情
    @RequestMapping("/selectById")
    @ResponseBody
    public Brand selectById(Integer id) {
        Brand brand = brandService.selectById(id);
        if (brand != null) {
            return brand;
        } else {
            return null;
        }
    }

    @RequestMapping("/get/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Brand brand = brandService.getByid(id);
        Integer code = brand == null ? Code.GET_ERR : Code.GET_0K;
        String msg = brand == null ? "没有找到" : "";
//        if (id == 2) {
//            throw new BusinessException(Code.GET_ERR, "查找过程出现问题,请联系管理员 ", null);
//        }
        Result result = new Result(code, msg, brand);
        return result;
    }
}
